package cpa.prowon.gui.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.MessageBox;

import pho.helper.SuffixFileNameFilter;
import cpa.prowon.Application;
import cpa.prowon.gui.MainWindow;
import cpa.prowon.image.ImageContainer;

/**
 * Import images from a folder
 * @author Chris
 */
public class ImportFromFolderAction extends ImportAction {

	/**
	 * Constructor
	 * @param MainWindow The window where the application is used
	 * @author Chris
	 */
	public ImportFromFolderAction(MainWindow w) {
		super(w);
	}

	/**
	 * Return the title key
	 * @return The title key
	 * @author Chris
	 */
	protected String getTitleKey() {
		return "menu_item_file_importfromfolder";
	}

	/**
	 * Return the tooltip key
	 * @return The tootip key
	 * @author Chris
	 */
	protected String getTooltipKey() {
		return "menu_item_file_importfromfolder_tooltip";
	}

	/**
	 * Return the image key
	 * @return The image key
	 * @author Chris
	 */
	protected String getImageKey() {
		return "file:icons/open.gif";
	}

	/**
	 * Return the list of files (images) that shall be imported
	 * @return Collection The collection of images that will be imported
	 * @author Chris
	 */
	protected Collection getImagesToImport() {

		// Initialize the image list
		ArrayList imageList = new ArrayList ();		

		// Prompt for the directory
		DirectoryDialog dialog = new DirectoryDialog (window.getShell());
		dialog.setMessage(Application.getInstance().getResourceBundle().getString("msg_selectdirectorytoimportimages"));
		dialog.setText(Application.getInstance().getResourceBundle().getString("title_selectdirectory"));
		String result = dialog.open ();
		
		// Return if no directory was chosen
		if (result == null) return imageList;

		// Ask the user whether or not he wants to include subfolders
		MessageBox msgBox = new MessageBox(window.getShell(), SWT.YES | SWT.NO);
		msgBox.setText(Application.getInstance().getResourceBundle().getString("title_includesubfolders"));
		msgBox.setMessage(Application.getInstance().getResourceBundle().getString("msg_includesubfolders"));
		int answer = msgBox.open();
		
		// If the user has chosen no, then return
		boolean includeSubfolders = false;
		if (answer == SWT.YES) {
			includeSubfolders = true;
		}
		
		// Create the image list
		File dir = new File(result);
		
		// Add the images from this directory to the list
		imageList.addAll(getImagesFromDirectory(dir, includeSubfolders));

		// Return the image list
		return imageList;
		
	}

	/**
	 * Add the appropriate files from the given folder
	 * @param File The directory that shall be checked for images
	 * @param boolean Indicate true to check subfolders as well
	 * @return A collection of images in a directory
	 * @author Chris
	 */
	protected Collection getImagesFromDirectory(File dir, boolean includeSubfolders) {

		// Initialize the image list
		ArrayList imageList = new ArrayList ();		

		// Add the images to the image list
		File[] fileArray = dir.listFiles(new SuffixFileNameFilter (".jpeg | .jpg" ));
		
		// Return immediately if nothing was returned
		if (fileArray == null) {
			return imageList;
		}
		
		// Go through the files and add them
		List fileList = Arrays.asList(fileArray);
		Iterator files = fileList.iterator();
		while (files.hasNext()) {
			ImageContainer ic = new ImageContainer((File) files.next());
			if (ic.isValid()) {
				imageList.add(ic);
			}
		}
		
		// Iterate through subfolders if necessary
		if (includeSubfolders == true) {
			File[] allFilesList = dir.listFiles();
			for (int i=1; i <= allFilesList.length; i++) {
				if (allFilesList[i-1].isDirectory()) {
					imageList.addAll(getImagesFromDirectory(allFilesList[i-1], includeSubfolders));
				}
			}
		}
		
		// Return the images
		return imageList;
		
	}
	
}
